<template>
  <div>
    <!-- 搜索 -->

    <van-search
      @input="search"
      v-model="value"
      placeholder="请输入搜索关键词"
    />

    <div v-if="value == ''" class="search_wrap">
      <div class="hot_title">热门搜索</div>
      <div class="hot_name_wrap">
        <div
          @click="search(item.first)"
          v-for="(item, index) in hotList"
          :key="index"
          class="hot_item"
        >
          {{ item.first }}
        </div>
      </div>
    </div>

    <!-- 最佳匹配 -->
    <div v-else class="search_wrap">
      <div class="hot_title">最佳匹配</div>
      <van-cell-group>
        <music-cell
          v-for="item in searchList"
          :key="item.id"
          :id="item.id"
          :title="item.name"
          :label="`${item.artists[0].name}-${item.name}`"
        ></music-cell>
      </van-cell-group>
    </div>
  </div>
</template>

<script>
import { hotAPI, searchAPI } from "@/API/search";

export default {
  methods: {
    async search(name) {
      if (name.trim() == "") return;
      this.value = name;
      const res = await searchAPI({
        keywords: this.value,
      });

      this.searchList = res.data.result.songs;
    },
  },
  async created() {
    const res = await hotAPI();
    // console.log(res);
    this.hotList = res.data.result.hots;
    //搜索框
  },
  data() {
    return {
      value: "",
      hotList: [],
      searchList: [],
    };
  },
};
</script>

<style>
/* 搜索容器的样式 */
.search_wrap {
  padding: 10px;
}

/*热门搜索文字标题样式 */
.hot_title {
  font-size: 12px;
  color: #666;
}

/* 热搜词_容器 */
.hot_name_wrap {
  margin: 10px 0;
}

/* 热搜词_样式 */
.hot_item {
  display: inline-block;
  height: 35px;
  margin-right: 20px;
  margin-bottom: 15px;
  padding: 3px 10px;
  font-size: 14px;
  line-height: 35px;
  color: #333;
  border-color: #d3d4da;
  border-radius: 20px;
  border: 1px solid #d3d4da;
}
</style>